﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ElementumForever.Utility;
using ElementumForever.Editer;
using ElementumForever.Enums;

namespace ElementumForever
{
    public partial class MainForm : Form
    {
        public MainFormModel PresentationModel { get; set; }
        private PropertyForm objectPropertyForm;
        private SceneStructureForm structureForm;
        private ContextMenuStrip contextMenuStrip;

        public MainForm()
        {
            InitializeComponent();
            XnaPanel.Size = new System.Drawing.Size(GraphicConstant.WINDOW_WIDTH,GraphicConstant.WINDOW_HEIGHT);
            PresentationModel = new MainFormModel(this);

            objectPropertyForm = new PropertyForm(this);
            objectPropertyForm.Show();
            structureForm = new SceneStructureForm();
            structureForm.Show();

            this.XnaPanel.MouseMove += this.PresentationModel.OnMouseMove;
            this.XnaPanel.MouseDown += this.PresentationModel.OnMouseDown;
            this.XnaPanel.MouseUp += this.PresentationModel.OnMouseUp;
            this.XnaPanel.MouseClick += this.PresentationModel.OnMouseClick;

            //
            contextMenuStrip = new ContextMenuStrip();
            contextMenuStrip.Opening += PresentationModel.OnOpenContextMenu;

            this.XnaPanel.ContextMenuStrip = contextMenuStrip;
            //

            cutToolStripMenuItem.Tag = MainFormModel.EditOptions.Cut;
            copyToolStripMenuItem.Tag = MainFormModel.EditOptions.Copy;
            pasteToolStripMenuItem.Tag = MainFormModel.EditOptions.Paste;


        }

        public void SetSceneProperty(object property)
        {
            mySceneProperty.SelectedObject = property;
        }

        public void RefreshPropertyView()
        {
            mySceneProperty.Refresh();
            objectPropertyForm.RefreshPropertyView();
        }

        public void SetObjectProperty(object property)
        {
            Console.WriteLine("SetObjectProperty");
            objectPropertyForm.SetPropertyTarget(property);
        }

        public void SetTreeData(EntityInfo data) {
            this.structureForm.SetTreeData(data);
        }

        public Control Panel
        {
            get { return XnaPanel; }
        }

        private void openLuabutton_Click(object sender, EventArgs e)
        {
            string filePath = PresentationModel.readLua();
            SceneFilePath.Text = filePath;
        }

        private void saveAsLuabutton_Click(object sender, EventArgs e)
        {
            PresentationModel.SaveAsLua();
        }

        private void redoToolButton_Click(object sender, EventArgs e)
        {
            PresentationModel.CManager.Redo();
            UpdateStatus();
        }

        private void undoToolButton_Click(object sender, EventArgs e)
        {
            PresentationModel.CManager.Undo();
            UpdateStatus();
        }

        public void UpdateStatus()
        {
            redoToolButton.Enabled = PresentationModel.CManager.EnableRedo;
            undoToolButton.Enabled = PresentationModel.CManager.EnableUndo;
            RefreshPropertyView();
        }

        private void newToolButton_Click(object sender, EventArgs e)
        {
            PresentationModel.NewScene();
        }

        private void openItemMenuButton_Click(object sender, EventArgs e)
        {
            PresentationModel.OpenItenMenu();
        }

        public void propertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            PresentationModel.Update();
        }

        private void onEditOptionClick(object sender, EventArgs e)
        {
            PresentationModel.OnEditOptionClick(sender, e);
        }

    }
}
